class Solution {
public:
    vector<bool> vis;
    vector<vector<int>> g;
    int cnt1 = 0;
    int cnt2 = 0;

    void dfs(int cur)
    {
        vis[cur] = true;
        cnt1++;
        cnt2 += g[cur].size();
        for(int i = 0;i < g[cur].size();i++)
        {
            int next = g[cur][i];
            if(vis[next]) continue;
            dfs(next);
        }
    }

    int countCompleteComponents(int n, vector<vector<int>>& edges) {
        vis.resize(n);
        g.resize(n);

        for(auto& e : edges)
        {
            int u = e[1];
            int v = e[0];
            g[u].push_back(v);
            g[v].push_back(u);
        }

        int ans = 0;
        for(int i = 0;i < n;i++)
        {
            if(vis[i]) continue;
            cnt1 = 0;
            cnt2 = 0;
            dfs(i);

            if(cnt1 * (cnt1 - 1) == cnt2) ans++;
        }
        return ans;
    }
};